package com.yzt.zhuju.utils.recyclerutils;

import android.graphics.Rect;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.annotation.Px;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

/**
 * author : bob
 * date   : 2022/12/1
 * desc   :
 */
public class GridItemDecoration extends RecyclerView.ItemDecoration {

    private final Builder mBuilder;

    public GridItemDecoration(Builder builder) {
        mBuilder = builder;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        final RecyclerView.ViewHolder holder = parent.getChildViewHolder(view);
        if (holder == null) {
            return;
        }
        boolean headItem = mBuilder.headViewExist && holder.getItemViewType() == mBuilder.headItemViewType;
        if (headItem || ignore(holder.getItemViewType())) {
            //头视图
            return;
        }

        int viewLayoutPosition = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition();
        if (mBuilder.headViewExist) {
            //排除Head
            viewLayoutPosition -= 1;
        }
        if (viewLayoutPosition < 0) {
            return;
        }
        int bottom = 0;
        int top = 0;
        final int spanCount = getSpanCount(parent);
        if (mBuilder.showFirstDivider && viewLayoutPosition < spanCount) {
            //第一行
            top += mBuilder.divider;
        }
        final int childCount = parent.getAdapter().getItemCount();
        final int column = viewLayoutPosition % spanCount;

        int left = column * mBuilder.divider / spanCount;
        int right = mBuilder.divider - (column + 1) * mBuilder.divider / spanCount;

        //是否是最后一行
        final boolean isLastRaw = isLastRaw(parent, viewLayoutPosition, spanCount, childCount);

        if (isLastRaw) {
            if (mBuilder.showLastDivider) {
                bottom = mBuilder.divider;
            }
        } else {
            //接下来的SpanCount中不存在其他ignore
            for (int i = 1; i <= spanCount - column; i++) {
                int position = (mBuilder.headViewExist ? viewLayoutPosition + 1 : viewLayoutPosition) + i;
                if (position >= parent.getAdapter().getItemCount()) {
                    break;
                }
                if (ignore(parent.getAdapter().getItemViewType(position))) {
                    if (mBuilder.showLastDivider) {
                        bottom = mBuilder.divider;
                    }
                    outRect.set(left, top, right, bottom);
                    marginOffsets(outRect, spanCount, viewLayoutPosition);
                    return;
                }
            }
            bottom = mBuilder.divider;
        }

        outRect.set(left, top, right, bottom);
        marginOffsets(outRect, spanCount, viewLayoutPosition);
    }

    private void marginOffsets(Rect outRect, int spanCount, int itemPosition) {
        if (mBuilder.marginRight == 0 && mBuilder.marginLeft == 0) {
            return;
        }

        int itemShrink = (mBuilder.marginLeft + mBuilder.marginRight) / spanCount;
        outRect.left += (mBuilder.marginLeft - (itemPosition % spanCount) * itemShrink);

        outRect.right += ((itemPosition % spanCount) + 1) * itemShrink - mBuilder.marginLeft;
    }

    private boolean ignore(int itemViewType) {
        if (!isEmpty(mBuilder.ignoreItemViewType)) {
            for (int type : mBuilder.ignoreItemViewType) {
                if (type == itemViewType) {
                    return true;
                }
            }
        }
        return false;
    }

    public static boolean isEmpty(@Nullable Object object) {
        return object == null;
    }

    /**
     * 获取列数
     */
    private int getSpanCount(RecyclerView parent) {
        int spanCount = -1;
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            spanCount = ((StaggeredGridLayoutManager) layoutManager)
                    .getSpanCount();
        }
        return spanCount;
    }

    /**
     * 是否是最后一行
     */
    private boolean isLastRaw(RecyclerView parent, int pos, int spanCount, int childCount) {
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            int remainCount = childCount % spanCount;
            if (remainCount == 0) {
                //最后一行全部不绘制;
                return pos >= childCount - spanCount;
            } else {
                //最后一排不完整;
                return pos >= childCount - remainCount;
            }
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            int orientation = ((StaggeredGridLayoutManager) layoutManager).getOrientation();
            if (orientation == StaggeredGridLayoutManager.VERTICAL) {
                int remainCount = childCount % spanCount;
                if (remainCount == 0) {
                    //最后一行全部不绘制;
                    return pos >= childCount - spanCount;
                } else {//最后一排不完整;
                    return pos >= childCount - remainCount;
                }
            } else {
                return (pos + 1) % spanCount == 0;
            }
        }
        return false;
    }

    public static class Builder {
        private int divider;
        private int marginLeft, marginRight;

        private int[] ignoreItemViewType = new int[]{};

        private int headItemViewType = -1;
        private boolean headViewExist = false;

        private boolean showFirstDivider = false;
        private boolean showLastDivider = false;

        public Builder() {
        }

        /**
         * 设置divider的宽度
         */
        public Builder divider(@Px int divider) {
            this.divider = divider;
            return this;
        }

        /**
         * 设置剔除HeadView的RecyclerView左右两边的外间距
         */
        public Builder margin(@Px int marginLeft, @Px int marginRight) {
            this.marginLeft = marginLeft;
            this.marginRight = marginRight;
            return this;
        }

        /**
         * 需要忽略的类型
         */
        public Builder ignoreItemViewType(int... itemViewTypes) {
            this.ignoreItemViewType = itemViewTypes;
            return this;
        }

        /**
         * Head的类型
         */
        public Builder headItemViewType(int headItemViewType) {
            this.headItemViewType = headItemViewType;
            this.headViewExist = true;
            return this;
        }

        /**
         * 第一行数据和Top的divider是否需要显示
         */
        public Builder showFirstDivider(boolean showFirstDivider) {
            this.showFirstDivider = showFirstDivider;
            return this;
        }

        /**
         * 第一行数据和Bottom的divider是否需要显示
         */
        public Builder showLastDivider(boolean showLastDivider) {
            this.showLastDivider = showLastDivider;
            return this;
        }

        public GridItemDecoration build() {
            return new GridItemDecoration(this);
        }
    }
}
